<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\admin\model\Custom;

class SolitaireController extends CommonController {/* 接龙活动 */
    /*
     * 接龙活动列表
     * author xfwed 
     * date 2021.08.04
     */

    public function solitaireList() {
        $name = input('param.name');
        $status = input('param.status'); //0全部1未开始2进行中3已结束
        $type = input('param.type'); //0全部1项目接龙2商品接龙
        $start_time = input('param.start_time');
        $end_time = input('param.end_time');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($name) {
            $where['name'] = ['like', '%' . $name . '%'];
        }
        if ($status == 1) {
            $where['start_time'] = ['>', date('Y-m-d H:i:s')];
        } elseif ($status == 2) {
            $where['start_time'] = ['<', date('Y-m-d H:i:s')];
            $where['end_time'] = ['>', date('Y-m-d H:i:s')];
        } elseif ($status == 3) {
            $where['end_time'] = ['<', date('Y-m-d H:i:s')];
        }
        if ($type) {
            $where['type'] = $type;
        }
        if ($start_time && $end_time) {
            $where['start_time'] = ['between', [$start_time, $end_time]];
        }

        $where = isset($where) ? $where : [];
        $list = Db::name('solitaire')
                ->where($where)
                ->field('id,name,start_time,end_time,type,add_time,update_time')
                ->paginate($perPage)
                ->each(function($item, $key) {
            $start_time = strtotime($item['start_time']);
            $end_time = strtotime($item['end_time']);
            if ($start_time > time()) {
                $status = 1;
            } elseif ($start_time < time() && $end_time > time()) {
                $status = 2;
            } elseif ($end_time < time()) {
                $status = 3;
            }
            $item['status'] = $status;
            return $item;
        });

        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 添加接龙活动
     * author xfwed
     * date 2021.08.04
     */

    public function addSolitaire() {
        $rule = [
            'name' => 'require',
            'start_time' => 'require',
            'end_time' => 'require',
            'num' => 'require',
            'limit_num' => 'require',
            'false_visit_gap' => 'require',
            'false_solitaire_gap' => 'require',
            'content' => 'require',
            'type' => 'require',
            'cun_id' => 'require',
            'lucky_bag_name' => 'require',
            'lucky_bag_title' => 'require',
            'lucky_bag_spec' => 'require',
            'lucky_bag_price' => 'require',
            'lucky_bag_original_price' => 'require',
            'lucky_bag_img' => 'require',
            'lucky_bag_content' => 'require',
            'front_num' => 'require',
            'red_money' => 'require',
        ];
        $msg = [
            'name.require' => '活动名称必须',
            'start_time.require' => '活动开始时间必须',
            'end_time.require' => '活动结束时间必须',
            'num.require' => '可接龙总次数必须',
            'limit_num.require' => '每人可接龙次数必须',
            'false_visit_gap.require' => '假浏览次数 每隔几分钟增加一次必须',
            'false_solitaire_gap.require' => '假接龙次数 每隔几分钟增加一次必须',
            'content.require' => '接龙详情必须',
            'type.require' => '接龙类型必须',
            'cun_id.require' => '活动消费代金券id必须',
            'lucky_bag_name.require' => '福袋名称必须',
            'lucky_bag_title.require' => '福袋标题必须',
            'lucky_bag_spec.require' => '福袋规格必须',
            'lucky_bag_price.require' => '福袋价格必须',
            'lucky_bag_original_price.require' => '福袋原价必须',
            'lucky_bag_img.require' => '福袋图片必须',
            'lucky_bag_content.require' => '福袋详情必须',
            'front_num.require' => '前几个接龙的用户 可以获得红包必须',
            'red_money.require' => '前几个接龙的用户 可以获得的红包金额必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        Db::startTrans();
        $solitaireData['name'] = $all['name'];
        $solitaireData['start_time'] = $all['start_time'];
        $solitaireData['end_time'] = $all['end_time'];
        $solitaireData['num'] = $all['num'];
        $solitaireData['limit_num'] = $all['limit_num'];
        $solitaireData['false_visit_gap'] = $all['false_visit_gap'];
        $solitaireData['false_solitaire_gap'] = $all['false_solitaire_gap'];
        $solitaireData['content'] = $all['content'];
        $solitaireData['type'] = $all['type'];
        $solitaireData['cun_id'] = $all['cun_id'];
        $solitaireData['lucky_bag_name'] = $all['lucky_bag_name'];
        $solitaireData['lucky_bag_title'] = $all['lucky_bag_title'];
        $solitaireData['lucky_bag_spec'] = $all['lucky_bag_spec'];
        $solitaireData['lucky_bag_price'] = $all['lucky_bag_price'];
        $solitaireData['lucky_bag_original_price'] = $all['lucky_bag_original_price'];
        $solitaireData['lucky_bag_img'] = $all['lucky_bag_img'];
        $solitaireData['lucky_bag_content'] = $all['lucky_bag_content'];
        $solitaireData['front_num'] = $all['front_num'];
        $solitaireData['red_money'] = $all['red_money'];
        $solitaireData['add_time'] = date('Y-m-d H:i:s');
        $solitaireData['update_time'] = date('Y-m-d H:i:s');
        $insertSolitaire = Db::name('solitaire')->insertGetId($solitaireData);

        $a = [['type' => 1, 'other_id' => 1, 'num' => 1, 'not_special_odds' => 100, 'end_special_odds' => 100, 'special_odds' => 100], ['type' => 1, 'other_id' => 1, 'num' => 1, 'not_special_odds' => 100, 'end_special_odds' => 100, 'special_odds' => 100]];
        //print_r(json_encode($a));die;
        //[{"type":1,"other_id":1,"num":1,"not_special_odds":100,"end_special_odds":100,"special_odds":100},{"type":1,"other_id":1,"num":1,"not_special_odds":100,"end_special_odds":100,"special_odds":100}]
        $solitaireInfo = json_decode($all['solitaire_info'], TRUE);
        foreach ($solitaireInfo as $k => $v) {
            $solitaireInfoData = [
                'solitaire_id' => $insertSolitaire,
                'type' => $v['type'],
                'other_id' => $v['other_id'],
                'num' => $v['num'],
                'not_special_odds' => $v['not_special_odds'],
                'end_special_odds' => $v['end_special_odds'],
                'special_odds' => $v['special_odds'],
                'add_time' => date('Y-m-d H:i:s'),
            ];
            $insertSolitaireInfo = Db::name('solitaire_info')->insertGetId($solitaireInfoData);
        }
        if ($insertSolitaire && $insertSolitaireInfo) {
            Db::commit();
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            Db::rollback();
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 编辑接龙活动
     * author xfwed
     * date 2021.08.04
     */

    public function saveSolitaire() {
        $requestType = input('param.requestType');
        $id = input('param.id');
        if ($requestType == 1) {//1修改前获取信息 2提交修改
            $row = Db::name('solitaire')->where('id', $id)->find();
            $row['solitaire_info'] = Db::name('solitaire_info')->where('is_del',2)->where('solitaire_id', $id)->select();
            return $this->ajaxReturn(0, '查询成功', $row);
        } else {
            $rule = [
                'name' => 'require',
                'start_time' => 'require',
                'end_time' => 'require',
                'num' => 'require',
                'limit_num' => 'require',
                'false_visit_gap' => 'require',
                'false_solitaire_gap' => 'require',
                'content' => 'require',
                'type' => 'require',
                'cun_id' => 'require',
                'lucky_bag_name' => 'require',
                'lucky_bag_title' => 'require',
                'lucky_bag_spec' => 'require',
                'lucky_bag_price' => 'require',
                'lucky_bag_original_price' => 'require',
                'lucky_bag_img' => 'require',
                'lucky_bag_content' => 'require',
                'front_num' => 'require',
                'red_money' => 'require',
            ];
            $msg = [
                'name.require' => '活动名称必须',
                'start_time.require' => '活动开始时间必须',
                'end_time.require' => '活动结束时间必须',
                'num.require' => '可接龙总次数必须',
                'limit_num.require' => '每人可接龙次数必须',
                'false_visit_gap.require' => '假浏览次数 每隔几分钟增加一次必须',
                'false_solitaire_gap.require' => '假接龙次数 每隔几分钟增加一次必须',
                'content.require' => '接龙详情必须',
                'type.require' => '接龙类型必须',
                'cun_id.require' => '活动消费代金券id必须',
                'lucky_bag_name.require' => '福袋名称必须',
                'lucky_bag_title.require' => '福袋标题必须',
                'lucky_bag_spec.require' => '福袋规格必须',
                'lucky_bag_price.require' => '福袋价格必须',
                'lucky_bag_original_price.require' => '福袋原价必须',
                'lucky_bag_img.require' => '福袋图片必须',
                'lucky_bag_content.require' => '福袋详情必须',
                'front_num.require' => '前几个接龙的用户 可以获得红包必须',
                'red_money.require' => '前几个接龙的用户 可以获得的红包金额必须',
            ];
            $all = input('post.');
            $validate = new Validate($rule, $msg);

            if (!$validate->check($all)) {
                $error = $validate->getError();
                return $this->ajaxReturn(1, $error, null);
            }

            Db::startTrans();
            $solitaireData['name'] = $all['name'];
            $solitaireData['start_time'] = $all['start_time'];
            $solitaireData['end_time'] = $all['end_time'];
            $solitaireData['num'] = $all['num'];
            $solitaireData['limit_num'] = $all['limit_num'];
            $solitaireData['false_visit_gap'] = $all['false_visit_gap'];
            $solitaireData['false_solitaire_gap'] = $all['false_solitaire_gap'];
            $solitaireData['content'] = $all['content'];
            $solitaireData['type'] = $all['type'];
            $solitaireData['cun_id'] = $all['cun_id'];
            $solitaireData['lucky_bag_name'] = $all['lucky_bag_name'];
            $solitaireData['lucky_bag_title'] = $all['lucky_bag_title'];
            $solitaireData['lucky_bag_spec'] = $all['lucky_bag_spec'];
            $solitaireData['lucky_bag_price'] = $all['lucky_bag_price'];
            $solitaireData['lucky_bag_original_price'] = $all['lucky_bag_original_price'];
            $solitaireData['lucky_bag_img'] = $all['lucky_bag_img'];
            $solitaireData['lucky_bag_content'] = $all['lucky_bag_content'];
            $solitaireData['front_num'] = $all['front_num'];
            $solitaireData['red_money'] = $all['red_money'];
            $solitaireData['update_time'] = date('Y-m-d H:i:s');
            $insertSolitaire = Db::name('solitaire')
                    ->where('id', $id)
                    ->update($solitaireData);
            //删除现有的所有权益
            $delSolitaireInfo = Db::name('solitaire_info')
                    ->where('solitaire_id', $id)
                    ->update(['is_del' => 1]);

            $solitaireInfo = json_decode($all['solitaire_info'], TRUE);
            foreach ($solitaireInfo as $k => $v) {
                $solitaireInfoData = [
                    'solitaire_id' => $id,
                    'type' => $v['type'],
                    'other_id' => $v['other_id'],
                    'num' => $v['num'],
                    'not_special_odds' => $v['not_special_odds'],
                    'end_special_odds' => $v['end_special_odds'],
                    'special_odds' => $v['special_odds'],
                    'add_time' => date('Y-m-d H:i:s'),
                ];
                $insertSolitaireInfo = Db::name('solitaire_info')->insertGetId($solitaireInfoData);
            }
            if ($insertSolitaire && $insertSolitaireInfo && $delSolitaireInfo) {
                Db::commit();
                return $this->ajaxReturn(0, '编辑成功', null);
            } else {
                Db::rollback();
                return $this->ajaxReturn(3, '编辑失败', null);
            }
        }
    }

    /*
     * 接龙订单列表
     * author xfwed 
     * date 2021.08.04
     */

    public function solitaireOrderList() {
        $id = input('param.id');//活动id  从活动列表进来传
        $name = input('param.name'); //活动名称
        $start_time = input('param.start_time'); //购买开始时间
        $end_time = input('param.end_time'); //购买结束时间
        $ord_num = input('param.ord_num'); //订单编号
        $ctm_name = input('param.ctm_name'); //用户昵称
        $ctm_phone = input('param.ctm_phone'); //用户手机号
        $tname = input('param.tname'); //推广者姓名
        $tphone = input('param.tphone'); //推广者手机号
        $ttype = input('param.ttype'); //推广者类型 1美容师 2店铺 3平台
        $status = input('param.status');//订单状态 订单状态 -1全部 0已取消 1待支付 2已完成=已支付 3已退款
        $vip_status = input('param.vip_status');//客户类型 0新客户 1普通用户 2特权客户 3体验特权用户
        $exclusive_store_id = input('param.exclusive_store_id');//专属店铺id

        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if($id){
            $where['so.id'] = $id;
        }
        if ($name) {
            $where['so.name'] = ['like', '%' . $name . '%'];
        }
        if ($start_time && $end_time) {
            $where['o.pay_time'] = ['between', [$start_time, $end_time]];
        }
        if($ord_num){
            $where['o.ord_num'] = $ord_num;
        }
        
        if ($ctm_name) {
            $where['c.custom_name'] = ['like', '%' . $ctm_name . '%'];
        }
        if ($ctm_phone) {
            $where['c.custom_phone'] = $ctm_phone;
        }
        if($ttype == 1){
            $where['osd.staff_id'] = ['<>',0];
            if($tname){
                $where['st.staff_name'] = ['like','%'.$tname.'%'];
            }
            if($tphone){
                $where['st.telephone'] = $tphone;
            }
        }elseif ($ttype == 2) {
            $where['osd.store_id'] = ['<>',0];
            if($tname){
                $where['s.store_name'] = ['like','%'.$tname.'%'];
            }
            if($tphone){
                $where['s.keeper_phone'] = $tphone;
            }
        }elseif ($ttype == 3) {
            $where['osd.staff_id'] = 0;
            $where['osd.store_id'] = 0;
        }
        if(preg_match("/^[0-9][0-9]*$/" ,$status)){
            $where['o.status'] = $status;
        }
        if(preg_match("/^[0-9][0-9]*$/" ,$vip_status)){
            $where['c.vip_status'] = $vip_status;
        }
        if($exclusive_store_id){
            $where['c.exclusive_store_id'] = $exclusive_store_id;
        }
        
        $where['o.type'] = 9;
        $list = Db::name('order')
                ->alias('o')
                ->join('custom c', 'c.id = o.ctm_id', 'left')
                ->join('coupon co', 'co.id = o.cun_id', 'left')
                ->join('order_solitaire_detail osd', 'osd.order_id = o.id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->join('store cs','cs.id = c.exclusive_store_id','left')
                ->where($where)
                ->field('o.id,o.ord_num,o.real_money,o.pay_money,o.pay_time,o.note,o.status,c.custom_phone,c.custom_name,c.vip_status,cs.store_name ctm_store_name,co.money,so.name,so.lucky_bag_name,s.store_name,s.keeper_phone,st.staff_name,st.telephone,count(osd.id) num,osd.staff_id,osd.store_id')
                ->group('o.id')
                ->order('o.id desc')
                ->paginate($perPage)
                ->each(function($item, $key) {
                    $staff_id = $item['staff_id'];
                    $store_id = $item['store_id'];
                    if ($staff_id) {
                        $ttype = 1;
                        $tname = $item['staff_name'];
                        $tphone = $item['telephone'];
                    } elseif ($store_id) {
                        $ttype = 2;
                        $tname = $item['store_name'];
                        $tphone = $item['keeper_phone'];
                    }  else {
                        $ttype = 3;
                        $tname = '平台';
                        $tphone = '4008013680';
                    }
                    $item['ttype'] = $ttype;
                    $item['tname'] = $tname;
                    $item['tphone'] = $tphone;
                    unset($item['staff_name']);
                    unset($item['telephone']);
                    unset($item['store_name']);
                    unset($item['keeper_phone']);
                    unset($item['staff_id']);
                    unset($item['store_id']);
                    return $item;
        });
        
        $row = Db::name('order')
                ->alias('o')
                ->join('custom c', 'c.id = o.ctm_id', 'left')
                ->join('coupon co', 'co.id = o.cun_id', 'left')
                ->join('order_solitaire_detail osd', 'osd.order_id = o.id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('sum(o.real_money) all_real_money,sum(o.pay_money) all_pay_money')
                ->group('o.id')
                ->order('o.id desc')
                ->select();
        $data['list'] = $list; 
        
        $orderNum = Db::name('order')
                ->alias('o')
                ->join('custom c', 'c.id = o.ctm_id', 'left')
                ->join('coupon co', 'co.id = o.cun_id', 'left')
                ->join('order_solitaire_detail osd', 'osd.order_id = o.id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('o.id,o.ord_num,o.real_money,o.pay_money,o.pay_time,o.note,c.custom_phone,c.custom_name,co.money,so.name,so.lucky_bag_name,s.store_name,s.keeper_phone,st.staff_name,st.telephone,count(osd.id) num,osd.staff_id,osd.store_id')
                ->group('o.id')
                ->order('o.id desc')
                ->count();
        $data['row']['orderNum'] = $orderNum;
        $all_real_money = Db::name('order')
                ->alias('o')
                ->join('custom c', 'c.id = o.ctm_id', 'left')
                ->join('coupon co', 'co.id = o.cun_id', 'left')
                ->join('order_solitaire_detail osd', 'osd.order_id = o.id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('o.id,o.ord_num,o.real_money,o.pay_money,o.pay_time,o.note,c.custom_phone,c.custom_name,co.money,so.name,so.lucky_bag_name,s.store_name,s.keeper_phone,st.staff_name,st.telephone,count(osd.id) num,osd.staff_id,osd.store_id')
                ->sum('o.real_money');
        $data['row']['all_real_money'] = $all_real_money;
        $all_pay_money = Db::name('order')
                ->alias('o')
                ->join('custom c', 'c.id = o.ctm_id', 'left')
                ->join('coupon co', 'co.id = o.cun_id', 'left')
                ->join('order_solitaire_detail osd', 'osd.order_id = o.id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('o.id,o.ord_num,o.real_money,o.pay_money,o.pay_time,o.note,c.custom_phone,c.custom_name,co.money,so.name,so.lucky_bag_name,s.store_name,s.keeper_phone,st.staff_name,st.telephone,count(osd.id) num,osd.staff_id,osd.store_id')
            
                ->sum('o.pay_money');
        $data['row']['all_pay_money'] = $all_pay_money;
        
        
        return $this->ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 用户分享列表
     * author xfwed 
     * date 2021.08.04
     */

    public function solitaireCtmShareList() {
        $id = input('param.id');//活动id  从活动列表进来传
        $name = input('param.name'); //活动名称
        $start_time = input('param.start_time'); //购买开始时间
        $end_time = input('param.end_time'); //购买结束时间
        $ctm_name = input('param.ctm_name'); //用户昵称
        $ctm_phone = input('param.ctm_phone'); //用户手机号
        $tname = input('param.tname'); //推广者姓名
        $tphone = input('param.tphone'); //推广者手机号
        $ttype = input('param.ttype'); //推广者类型 1美容师 2店铺 3平台
        $yname = input('param.yname');//邀请人姓名
        $yphone = input('param.yphone');//邀请人手机号
        $is_buy = input('param.is_buy'); //0全部1已购买2未购买

        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;

        if($id){
            $where['so.id'] = $id;
        }
        if ($name) {
            $where['so.name'] = ['like', '%' . $name . '%'];
        }
        if ($start_time && $end_time) {
            $where['r.add_time'] = ['between', [$start_time, $end_time]];
        }
        if ($ctm_name) {
            $where['c.custom_name'] = ['like', '%' . $ctm_name . '%'];
        }
        if ($ctm_phone) {
            $where['c.custom_phone'] = $ctm_phone;
        }
        if($ttype == 1){
            $where['osd.staff_id'] = ['<>',0];
            if($tname){
                $where['st.staff_name'] = ['like','%'.$tname.'%'];
            }
            if($tphone){
                $where['st.telephone'] = $tphone;
            }
        }elseif ($ttype == 2) {
            $where['osd.store_id'] = ['<>',0];
            if($tname){
                $where['s.store_name'] = ['like','%'.$tname.'%'];
            }
            if($tphone){
                $where['s.keeper_phone'] = $tphone;
            }
        }elseif ($ttype == 3) {
            $where['osd.staff_id'] = 0;
            $where['osd.store_id'] = 0;
        }
        if ($yname) {
            $where['yc.custom_name'] = ['like', '%' . $yname . '%'];
        }
        if ($yphone) {
            $where['yc.custom_phone'] = $yphone;
        }
        if($is_buy){
            $having = 'is_buy = '.$is_buy;
        }  else {
            $having = 'is_buy in(1,2)';
        }
        
        $where['r.type'] = 7;
        $where['rpc.type'] = 4;
        $list = Db::name('redpacket')
                ->alias('r')
                ->join('custom c', 'c.id = r.ctm_id', 'left')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('custom yc','yc.id = rpc.ctm_id','left')
                ->join('order_solitaire_detail osd', 'osd.id = rpc.order_id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('r.id,r.add_time,c.custom_phone,c.custom_name,yc.custom_phone ycustom_phone,yc.custom_name ycustom_name,s.store_name,s.keeper_phone,st.staff_name,st.telephone,so.name,osd.staff_id,osd.store_id,'
                        . 'if((select count(o.id) is_buy from ai_order o LEFT JOIN ai_order_solitaire_detail osd on o.id=osd.order_id where o.ctm_id = r.ctm_id and o.status = 2 and o.type = 9 and osd.solitaire_id = so.id)>0,1,2) is_buy')
                ->having($having)
                ->order('r.id desc')
                ->limit($offset, $perPage)
                ->select();
        foreach ($list as $k => $v){
            $staff_id = $v['staff_id'];
            $store_id = $v['store_id'];
            if ($staff_id) {
                $ttype = 1;
                $tname = $v['staff_name'];
                $tphone = $v['telephone'];
            } elseif ($store_id) {
                $ttype = 2;
                $tname = $v['store_name'];
                $tphone = $v['keeper_phone'];
            }  else {
                $ttype = 3;
                $tname = '平台';
                $tphone = '4008013680';
            }
            $list[$k]['ttype'] = $ttype;
            $list[$k]['tname'] = $tname;
            $list[$k]['tphone'] = $tphone;
            unset($v['staff_name']);
            unset($v['telephone']);
            unset($v['store_name']);
            unset($v['keeper_phone']);
            unset($v['staff_id']);
            unset($v['store_id']);
        }
        $total = Db::name('redpacket')
                ->alias('r')
                ->join('custom c', 'c.id = r.ctm_id', 'left')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('custom yc','yc.id = rpc.ctm_id','left')
                ->join('order_solitaire_detail osd', 'osd.id = rpc.order_id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('r.id,r.add_time,c.custom_phone,c.custom_name,yc.custom_phone ycustom_phone,yc.custom_name ycustom_name,s.store_name,s.keeper_phone,st.staff_name,st.telephone,so.name,osd.staff_id,osd.store_id,'
                        . 'if((select count(o.id) is_buy from ai_order o LEFT JOIN ai_order_solitaire_detail osd on o.id=osd.order_id where o.ctm_id = r.ctm_id and o.status = 2 and o.type = 9 and osd.solitaire_id = so.id)>0,1,2) is_buy')
                ->having($having)
                ->select();
        //根据筛选，27个推广者，推广了28个用户，邀请了39个用户，3个购买，36个未购买；
        //推广者
        $tuiguang = Db::name('redpacket')
                ->alias('r')
                ->join('custom c', 'c.id = r.ctm_id', 'left')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('custom yc','yc.id = rpc.ctm_id','left')
                ->join('order_solitaire_detail osd', 'osd.id = rpc.order_id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('sum(if(osd.staff_id=0,0,1)) staffnum,sum(if(osd.store_id=0,0,1)) storenum,if((select count(o.id) is_buy from ai_order o LEFT JOIN ai_order_solitaire_detail osd on o.id=osd.order_id where o.ctm_id = r.ctm_id and o.status = 2 and o.type = 9 and osd.solitaire_id = so.id)>0,1,2) is_buy')
                ->having($having)
                ->group('osd.staff_id,osd.store_id')
                ->select();
        
        //推广用户
        $tuiguangctm = Db::name('redpacket')
                ->alias('r')
                ->join('custom c', 'c.id = r.ctm_id', 'left')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('custom yc','yc.id = rpc.ctm_id','left')
                ->join('order_solitaire_detail osd', 'osd.id = rpc.order_id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('if((select count(o.id) is_buy from ai_order o LEFT JOIN ai_order_solitaire_detail osd on o.id=osd.order_id where o.ctm_id = r.ctm_id and o.status = 2 and o.type = 9 and osd.solitaire_id = so.id)>0,1,2) is_buy')
                ->having($having)
                ->group('osd.ctm_id')
                ->select();
        //邀请用户数
        $yaoqingctm = Db::name('redpacket')
                ->alias('r')
                ->join('custom c', 'c.id = r.ctm_id', 'left')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('custom yc','yc.id = rpc.ctm_id','left')
                ->join('order_solitaire_detail osd', 'osd.id = rpc.order_id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('if((select count(o.id) is_buy from ai_order o LEFT JOIN ai_order_solitaire_detail osd on o.id=osd.order_id where o.ctm_id = r.ctm_id and o.status = 2 and o.type = 9 and osd.solitaire_id = so.id)>0,1,2) is_buy')
                ->having($having)
                ->group('r.ctm_id')
                ->select();
        //邀请用户数已购买
        $goumaictm = Db::name('redpacket')
                ->alias('r')
                ->join('custom c', 'c.id = r.ctm_id', 'left')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('custom yc','yc.id = rpc.ctm_id','left')
                ->join('order_solitaire_detail osd', 'osd.id = rpc.order_id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('if((select count(o.id) is_buy from ai_order o LEFT JOIN ai_order_solitaire_detail osd on o.id=osd.order_id where o.ctm_id = r.ctm_id and o.status = 2 and o.type = 9 and osd.solitaire_id = so.id)>0,1,2) is_buy')
                ->having('is_buy = 1')
                ->group('r.ctm_id')
                ->select();
        //邀请用户数未购买
        $weigoumaictm = Db::name('redpacket')
                ->alias('r')
                ->join('custom c', 'c.id = r.ctm_id', 'left')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('custom yc','yc.id = rpc.ctm_id','left')
                ->join('order_solitaire_detail osd', 'osd.id = rpc.order_id', 'left')
                ->join('solitaire so', 'so.id = osd.solitaire_id', 'left')
                ->join('store s', 's.id = osd.store_id', 'left')
                ->join('staff st', 'st.id = osd.staff_id', 'left')
                ->where($where)
                ->field('if((select count(o.id) is_buy from ai_order o LEFT JOIN ai_order_solitaire_detail osd on o.id=osd.order_id where o.ctm_id = r.ctm_id and o.status = 2 and o.type = 9 and osd.solitaire_id = so.id)>0,1,2) is_buy')
                ->having('is_buy = 2')
                ->group('r.ctm_id')
                ->select();
        $is_buy = input('param.is_buy'); //0全部1已购买2未购买
        if($is_buy == 0){
            $g = count($goumaictm); 
            $w = count($weigoumaictm); 
        }elseif ($is_buy == 1) {
            $g = count($goumaictm); 
            $w = 0; 
        }elseif ($is_buy == 2) {
            $g = 0; 
            $w = count($weigoumaictm); 
        }
        $data['list'] = $list; 
        $data['total'] = count($total); 
        if($tuiguang){
            $tnum = 0;
            for($i=0;$i<count($tuiguang);$i++){
                $tnum = $tnum + $tuiguang[$i]['staffnum']+$tuiguang[$i]['storenum'];
            }
        }  else {
            $tnum = 0;
        }
        $data['tnum'] = $tnum;
        $data['tctm'] = count($tuiguangctm); 
        $data['yctm'] = count($yaoqingctm); 
        $data['goumaictm'] = $g; 
        $data['weigoumaictm'] = $w; 
  
        
        return $this->ajaxReturn(0, '查询成功', $data);
    }

}
